ESP01s + blinker + 小度音响 实现客厅灯远程控制(二)

2022年2月8日 Jerry 3912 2023年11月4日

上一篇实现了使用 Blinker APP 控制继电器的开合,接下来继续关于小度音响的接入。

具体的文档介绍及示例代码可以看官网 https://diandeng.tech/doc/dueros

用在智能灯上面,基本的功能也只有 关 开,于是这里选择的的设备类型为:light

#define BLINKER_DUEROS_LIGHT

设备接收到命令后的回调函数:

void duerPowerState(const String & state)
{
    BLINKER_LOG("need set power state: ", state);

    if (state == BLINKER_CMD_ON) {
        digitalWrite(LED_BUILTIN, LOW);
        digitalWrite(0, HIGH);
        Button1.color("#6666ff");
        Button1.text("已开启");
        Button1.print("on");
        BlinkerDuerOS.powerState("on");
        BlinkerDuerOS.print();
        oState = true;
    }
    else if (state == BLINKER_CMD_OFF) {
        digitalWrite(LED_BUILTIN, HIGH);
        digitalWrite(0, LOW);
        Button1.color("#708090");
        Button1.text("已关闭");
        Button1.print("off");
        BlinkerDuerOS.powerState("off");
        BlinkerDuerOS.print();
        oState = false;
    }
}

接收到命令后再修改引脚高低电平的同时,也把按钮的状态及文字告知APP,这样交互性好一些。

查询命令的回调函数:

void duerQuery(int32_t queryCode)
{
    BLINKER_LOG("DuerOS Query codes: ", queryCode);

    switch (queryCode)
    {
        case BLINKER_CMD_QUERY_POWERSTATE_NUMBER  :
            BLINKER_LOG("DuerOS Query power state");
            BlinkerDuerOS.powerState(oState ? "on" : "off");
            BlinkerDuerOS.print();
            break;
        case BLINKER_CMD_QUERY_TIME_NUMBER  :
            BLINKER_LOG("DuerOS Query time");
            BlinkerDuerOS.time(millis());
            BlinkerDuerOS.print();
            break;
        default :
            BlinkerDuerOS.powerState(oState ? "on" : "off");
            BlinkerDuerOS.print();
            break;
    }
}

两个回调函数完成后,再setup启动函数中绑定即可

void setup()
{
    BlinkerDuerOS.attachPowerState(duerPowerState);
    BlinkerDuerOS.attachQuery(duerQuery);
}

烧写代码后,设备将会正常上线,在点灯 APP上看到上线。

接下来,在小度 APP上进行 blinker 账号的绑定与设备同步。

打开小度APP——右上角 添加设备——搜索 blinker——绑定账号。

绑定完成后点击同步设备,要保证设备是在线的,否则同步是不显示的。

同步完成后会显示刚刚的设备:

接下来,就可以使用语音进行控制了。最后,贴一下完整代码:记得修改 wifi 及 设备的authkey

#define BLINKER_WIFI

#define BLINKER_MIOT_LIGHT
#define BLINKER_DUEROS_LIGHT

#include <Blinker.h>

char auth[] = "xxx";
char ssid[] = "xxxxxeee";
char pswd[] = "233222eeee";

// 新建组件对象
BlinkerButton Button1("btn-abc");

bool oState = false;

void miotPowerState(const String & state)
{
    //BLINKER_LOG("need set power state: ", state);
    Serial.print(state);
    if (state == BLINKER_CMD_ON) {
        digitalWrite(LED_BUILTIN, HIGH);
        digitalWrite(0, LOW);
        Button1.color("#6666ff");
        Button1.text("已开启");
        Button1.print("on");
        BlinkerMIOT.powerState("on");
        BlinkerMIOT.print();
        oState = true;
    }
    else if (state == BLINKER_CMD_OFF) {
        digitalWrite(LED_BUILTIN, LOW);
        digitalWrite(0, HIGH);
        Button1.color("#708090");
        Button1.text("已关闭");
        Button1.print("off");
        BlinkerMIOT.powerState("off");
        BlinkerMIOT.print();
        oState = false;
    }
}

void miotQuery(int32_t queryCode)
{
    BLINKER_LOG("MIOT Query codes: ", queryCode);

    switch (queryCode)
    {
        case BLINKER_CMD_QUERY_ALL_NUMBER :
            BLINKER_LOG("MIOT Query All");
            BlinkerMIOT.powerState(oState ? "on" : "off");
            BlinkerMIOT.print();
            break;
        case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
            BLINKER_LOG("MIOT Query Power State");
            BlinkerMIOT.powerState(oState ? "on" : "off");
            BlinkerMIOT.print();
            break;
        default :
            BlinkerMIOT.powerState(oState ? "on" : "off");
            BlinkerMIOT.print();
            break;
    }
}

void duerPowerState(const String & state)
{
    BLINKER_LOG("need set power state: ", state);

    if (state == BLINKER_CMD_ON) {
        digitalWrite(LED_BUILTIN, LOW);
        digitalWrite(0, HIGH);
        Button1.color("#6666ff");
        Button1.text("已开启");
        Button1.print("on");
        BlinkerDuerOS.powerState("on");
        BlinkerDuerOS.print();
        oState = true;
    }
    else if (state == BLINKER_CMD_OFF) {
        digitalWrite(LED_BUILTIN, HIGH);
        digitalWrite(0, LOW);
        Button1.color("#708090");
        Button1.text("已关闭");
        Button1.print("off");
        BlinkerDuerOS.powerState("off");
        BlinkerDuerOS.print();
        oState = false;
    }
}

void duerQuery(int32_t queryCode)
{
    BLINKER_LOG("DuerOS Query codes: ", queryCode);

    switch (queryCode)
    {
        case BLINKER_CMD_QUERY_POWERSTATE_NUMBER  :
            BLINKER_LOG("DuerOS Query power state");
            BlinkerDuerOS.powerState(oState ? "on" : "off");
            BlinkerDuerOS.print();
            break;
        case BLINKER_CMD_QUERY_TIME_NUMBER  :
            BLINKER_LOG("DuerOS Query time");
            BlinkerDuerOS.time(millis());
            BlinkerDuerOS.print();
            break;
        default :
            BlinkerDuerOS.powerState(oState ? "on" : "off");
            BlinkerDuerOS.print();
            break;
    }
}

// 按下按键即会执行该函数
void button1_callback(const String & state)
{
    BLINKER_LOG("get button state: ", state);
    Serial.print("state");
    if(state == "on")
    {
      Serial.print("state ON");
      digitalWrite(LED_BUILTIN, LOW);
      digitalWrite(0, HIGH);
      Button1.color("#6666ff");
      Button1.text("已开启");
      Button1.print("on");
      oState = true;
    }
    else if(state == "off")
    {
      Serial.print("state OFF");
      digitalWrite(LED_BUILTIN, HIGH);
      digitalWrite(0, LOW);
      Button1.color("#708090");
      Button1.text("已关闭");
      Button1.print("off");
      oState = false;
    }
    else{}
}

void setup()
{
    // 初始化串口
    Serial.begin(9600);
    BLINKER_DEBUG.stream(Serial);
    BLINKER_DEBUG.debugAll();

    // 初始化有LED的IO
    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, LOW);

    pinMode(0, OUTPUT);
    digitalWrite(0, HIGH);

    // 初始化blinker
    Blinker.begin(auth, ssid, pswd);

    Button1.attach(button1_callback);

    BlinkerDuerOS.attachPowerState(duerPowerState);
    BlinkerDuerOS.attachQuery(duerQuery);

    BlinkerMIOT.attachPowerState(miotPowerState);
    BlinkerMIOT.attachQuery(miotQuery);
}

void loop() {
    Blinker.run();
}

ESP烧录:https://www.jerrycoding.com/article/esp01s-writer/


原创文章,转载请注明出处: https://jerrycoding.com/article/esp01-smart-light-2

微信
jerry微信赞助
支付宝
jerry支付宝赞助

您尚未登录,暂时无法评论。请先 登录 或者 注册

0 人参与 | 0 条评论